﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace wifi_test
{
    class cls_record
    {
        public enum recordstate {nouse, recerror, senderror, endtest, starttest};

        public struct record
        {
            public recordstate state;
            public string time;
            public string data;

            public record(recordstate param_state, string param_time, string param_data)
            {
                this.state = param_state;
                this.time = param_time;
                this.data = param_data;
            }
        };
        private int record_count = 0;
        private int recordspace = 10;

        record [] record_sp = new record[10];


        /// <summary>
        /// 把要记录的数据转成struct record格式，并加入到record_sp中去
        /// </summary>
        /// <param name="state">要记录的原因</param>
        /// <param name="data">记录的内容</param>
        public void sub_recordin(recordstate state, string data)
        {
            string xustr = System.DateTime.Now.ToString();
            if ((record_count + 1) > recordspace)
            {
                Array.Resize(ref record_sp, record_sp.Length + 10);
                recordspace += 10;
            }

            record tmp_record = new record(state, xustr, data);

            record_sp.SetValue(tmp_record, record_count);
            record_count += 1;
            if (record_count > 100)
            {
                sub_genrecordfile();
            }
        }


        /// <summary>
        /// 把相关的信息记录到文件中去同时清空record数组
        /// </summary>
        public void sub_genrecordfile()
        {
            if (record_count != 0)
            {
                string _FilePath = "ErrLog.txt";
                System.IO.FileStream oFileStream;
                if (!System.IO.File.Exists(_FilePath))
                {
                    oFileStream = new System.IO.FileStream(_FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
                }
                else
                {
                    oFileStream = new System.IO.FileStream(_FilePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
                }
                System.IO.StreamWriter oStreamWriter = new System.IO.StreamWriter(oFileStream, System.Text.Encoding.Default);

                string _FileContent = null;
                record tmp_record;

                int i;
                for (i = 0; i < record_count; i++)
                {
                    tmp_record = (record)record_sp.GetValue(i);
                    switch (tmp_record.state)
                    {
                        case recordstate.endtest:
                            _FileContent = "结束测试：" + tmp_record.time + " " + tmp_record.data;
                            break;

                        case recordstate.recerror:
                            _FileContent = "接收出错：" + tmp_record.time + " " + tmp_record.data;
                            break;

                        case recordstate.senderror:
                            _FileContent = "发送出错：" + tmp_record.time + " " + tmp_record.data;
                            break;

                        case recordstate.starttest:
                            _FileContent = "开始测试：" + tmp_record.time + " ";
                            break;
                    }
                    oStreamWriter.Write(_FileContent);
                    oStreamWriter.WriteLine();
                }

                record_sp = null;

                record_count = 0;
                recordspace = 10;

                record_sp = new record[10];

                oStreamWriter.Close();
                oFileStream.Close();
            }
        }
        
    }
}
